/**
 * @module LeftCtrol
 * @class LeftCtrol
 * @require RightCtrol
 */
define(function (require, exports, module) {

    //依赖右侧
    var RightCtrol = require('./rightCtrol');

    function LeftCtrol() {
        if (!(this instanceof LeftCtrol)) {
            return new LeftCtrol(opt);
        }

        this.startY = 0;
        this.moveY = 0;
        this.translateYValue = 0;

        this.ulHeight = 0;

        //全局变量
        window.getInfoId = 0;
    }

    LeftCtrol.prototype = {
        init: function () {

            this.getData();
            this.clickEventLi();
            this.useTouchEvent();
            this.getRightCtrolData();
        },
        getRightCtrolData:function () {
            var R = new RightCtrol();
            R.getData();
        },
        clickEventLi: function() {
            var mainMenu = $('#main-menu ul');

            // $('#main-menu ul').bind('li', 'tap', function () {
            //     console.log(11111122);
            // })

            console.log(1);
            mainMenu.off('tap').on('tap','li', function (e) {
                console.log(2);
                $('#main-menu ul li').removeClass('active');
                $(this).addClass('active');

                window.getInfoId = e.currentTarget.dataset.id;
                
                var R = new RightCtrol();
                R.getData();

                // this.getDataSub(e.currentTarget.dataset.id);
            })
            

        },
        //拉右侧数据

        //拉数据
        getData:function (id) {
            var mainMenu = $('#main-menu ul');
            var html = '';
            $.ajax({
                url: 'http://youyong.ba:8080/mock/5fed6c290d6d9c4f58824008/jd-category',
                success:function (res) {
                    console.log(res, '<---')

                    // res.data.forEach((v,k) => {
                       
                    //     if (k==0) {
                    //         //全局变量
                    //         window.getInfoId = v.id
                    //         html += '<li data-id="'+v.id+'" class="active"><a href="javascript:void(0)">'+v.title +'</a></li>';
                    //     } else {
                    //         html += '<li data-id="' +v.id+'"><a href="javascript:void(0)">'+v.title+'</a></li>';

                    //     }
                    // });

                    for (var i=0; i< res.data.length; i++) {
                        if (i==0) {
                                    //全局变量
                                    window.getInfoId = res.data[i].id
                                    html += '<li data-id="'+res.data[i].id+'" class="active"><a href="javascript:void(0)">'+res.data[i].title +'</a></li>';
                                } else {
                                    html += '<li data-id="' +res.data[i].id+'"><a href="javascript:void(0)">'+res.data[i].title+'</a></li>';
        
                                }
                    }

                    mainMenu.html(html);
                }
            })
        },
        useTouchEvent: function () {

            

            var mainMenu = $('#main-menu ul');
            var windowHeight = $(window).height();

            mainMenu.off('touchstart').on('touchstart', function (e) {

                if (mainMenu.height() < $(window).height()) {
                    return ;
                } else {
                    this.startY = e.touches[0].pageY;

                }
            }.bind(this));

            mainMenu.off('touchmove').on('touchmove', function (e) {

                if (mainMenu.height() < $(window).height()) {
                    return ;
                } else {
                    this.moveY = e.touches[0].pageY;
                    this.translateYValue += this.moveY - this.startY;
                    if (this.translateYValue > 150) {
                        this.translateYValue = 150;
                    }
    
                    if (mainMenu.height() - $(window).height() < Math.abs(this.translateYValue)) {
    
                        this.translateYValue = -(mainMenu.height() - $(window).height());
                    }
                    this.moveMenu();
                }

               

            }.bind(this));

            mainMenu.off('touchend').on('touchend', function (e) {

                if (mainMenu.height() < $(window).height()) {
                    return ;
                } else {
                    if (this.translateYValue > 0) {
                        this.translateYValue = 0;
                    }
                    this.moveMenu();
                }
                

            }.bind(this));


        },
        moveMenu: function () {
            var mainMenu = $('#main-menu ul');
            mainMenu[0].style.transform = "translateY(" + this.translateYValue + "px)";
        }
    }
    module.exports = LeftCtrol;

});